package com.tang.jwtdemo2.controller;

import com.auth0.jwt.interfaces.Payload;
import com.tang.jwtdemo2.pojo.CodeEnum;
import com.tang.jwtdemo2.pojo.ReturnMessage;
import com.tang.jwtdemo2.utils.JWTService;
import com.tang.jwtdemo2.utils.ReturnMessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import com.google.common.base.Objects;

@RestController
public class LoginController {
    Logger log = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private JWTService jwtService;

    @RequestMapping("/login")
    public ReturnMessage<Object> login(String loginName, String password, HttpServletRequest request) {
        if(valid(loginName,password)) {
            ReturnMessageUtil.error(CodeEnum.LOGINNAMEANDPWDERROR);
        }

        Map<String,String> userInfo = createUserInfoMap(loginName,password);
        String token = jwtService.createToken(userInfo, 1);

        ServletContext context = request.getServletContext();
        context.setAttribute(token, token);
        log.info("token:"+token);
        return ReturnMessageUtil.sucess(token);
    }

    @GetMapping("/logout")
    public ReturnMessage<?> logout(String token,String issuer,HttpServletRequest request) {
        ServletContext context = request.getServletContext();
        context.removeAttribute(token);
        return ReturnMessageUtil.sucess();
    }


    private Map<String,String> createUserInfoMap(String loginName, String password) {
        Map<String,String> userInfo = new HashMap<String,String>();
        userInfo.put("loginName", loginName);
        userInfo.put("password", password);
        return userInfo;
    }

    private boolean valid(String loginName, String password) {
        if(Objects.equal("tzs", loginName) && Objects.equal("123456", password) ) {
            return true;
        }

        return false;
    }

}
